//
//  BN_WeekRecipeViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-15.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_WeekRecipeViewController.h"
#import "BN_WeekFood.h"

@interface BN_WeekRecipeViewController () {
    BN_WeekFood *weekFoodData;
    MJRefreshHeaderView *_header;
}

@end

@implementation BN_WeekRecipeViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [weekFoodData.kinderGartenHelper cancelQueryFoodDetail];
    [weekFoodData.kinderGartenHelper cancelQueryFoodList];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [[NSUserDefaults standardUserDefaults] setInteger:FOOD forKey:USERDEFAULTS_KINDERSECTION];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    // UI
    self.tabBarController.tabBar.hidden = YES;
    
    // UI+64
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"每周食谱";
    
    [self schoolNavgationBar:3];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // 列表
    __weak typeof(self) weakSelf = self;
    weekFoodData = [[BN_WeekFood alloc]init];
    [weekFoodData toSchoolReloadList:^(BOOL isReload,BOOL isDele,NSArray *indexArr) {
        if (!isDele) {
            if (isReload) {
                [weakSelf cancelHintView];
                [weakSelf.weekRecipeTableView reloadData];
            }else {
                [weakSelf cancelHintView];
            }
        }else {
            [LZCHINT title:@"美食删除成功" type:LZCSuccess duration:3 places:HINT_PLACES callBack:NULL];
            [weakSelf.weekRecipeTableView reloadData];
        }

    }];
    self.weekRecipeTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT) style:UITableViewStylePlain];
    [_weekRecipeTableView setDecelerationRate:0.1];
    switch (MAIN_VERSION) {
        case 6:
            self.weekRecipeTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-44);
            break;
        default:
            
            self.weekRecipeTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, FULLSCREEN_HEIGHT);
            self.weekRecipeTableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.weekRecipeTableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
            break;
    }
    if ([self.weekRecipeTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.weekRecipeTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    self.weekRecipeTableView.delegate = weekFoodData;
    self.weekRecipeTableView.dataSource = weekFoodData;
    [self setExtraCellLineHidden:self.weekRecipeTableView];
    self.weekRecipeTableView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    [self.view addSubview:self.weekRecipeTableView];
    
    // 下拉刷新
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.weekRecipeTableView;
    header.delegate = weekFoodData;
    _header = header;
    
    // 小球提示
    [self viewLoadHintView];
    
    // 通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(openXQPage:) name:KKINDER_OPEN_FOOD_XQ object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(foodModelRefresh:) name:KSENDSUCCESS_AFTER_FOOD_REFRESH object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(foodDele:) name:K_SCHOOL_FOOD_DELE object:nil];
}

#pragma mark
#pragma mark Action

- (void)foodDele:(NSNotification *)notification {
    [weekFoodData foodModelRemove:notification.object];
}

- (void)foodModelRefresh:(NSNotification *)notification {
    [weekFoodData foodRefresh];
}

- (void)backRootVC {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goCreateVC {
    BN_CreateContentViewController *createVC = [[BN_CreateContentViewController alloc]init];
    [createVC toHintMessage:^(BOOL isSuccess) {
        [LZCHINT title:@"更新成功" type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
    }];
    [self.navigationController pushViewController:createVC animated:YES];
}

- (void)openXQPage:(NSNotification *)notification {
    /*NSString *foodId = notification.object;
    BN_ContentXQViewController *kindexqVC_ = [[BN_ContentXQViewController alloc]init];
    kindexqVC_.objctId = foodId;
    [self.navigationController pushViewController:kindexqVC_ animated:YES];*/
}

#pragma mark
#pragma mark 生命周期

- (void)releaseResource {
    
    [_header free];
    _header.delegate = nil;
    _header = nil;
    
    self.weekRecipeTableView.delegate = nil;
    self.weekRecipeTableView.dataSource = nil;
    self.weekRecipeTableView = nil;
    
    weekFoodData.reloadList = nil;
    [weekFoodData.kinderGartenHelper cancelQueryFoodList];
    weekFoodData.kinderGartenHelper = nil;
    weekFoodData = nil;

    [self cancelHintView];
}

- (void)dealloc
{
    [self releaseResource];
    NSLog(@"每周食谱释放...");
}
@end
